package Part2_字符流.FileReader_FileWriter;

import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * ClassName: Demo
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.19 18:16
 */

public class FileReaderWriterTest {
    /**
     * 读取文件内容
     */
    @Test
    public void test1() {
        File file1 = new File("hello.txt");

        FileReader fr1 = null;

        try {
            fr1 = new FileReader(file1);

            //读第一个字符：System.out.println((char) read);

            int read;
            while ((read = fr1.read())!=-1){
                System.out.print((char) read);
            }
        }

        catch (IOException e) {
            e.printStackTrace();
        }

        finally {
            try {
                //避免空指针
                if(fr1!=null)
                    fr1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test2() {
        //1、创建File对象
        File file = new File("hello.txt");

        //2、创建输入流
        FileReader fr1 = null;

        try {
            fr1 = new FileReader(file);

            //读第一个字符：System.out.println((char) read);

            //3、读操作
            char[] charBuffer = new char[4];
            int len;
            while ((len = fr1.read(charBuffer))!=-1){
                for(int i=0; i<len; i++){
                    System.out.print(charBuffer[i]);
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        finally {
            try {
                //4、关闭资源
                if(fr1!=null)
                    fr1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 写入指定文件
     */
    @Test
    public void test3() {
        //1、创建File对象
        File file =  new File("infoInput.txt");

        FileWriter fw = null;
        try {
            //2、创建输出流

            //覆盖文件使用的构造器
            fw = new FileWriter(file);
            //追加内容使用的构造器
            //fw = new FileWriter(file, true);

            //3、具体操作
            fw.write("24821783sdf\n");
            fw.write("33242153sdf\n");
            fw.write("44f35783sdf\n");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4、关闭资源
                if (fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 复制一份现存的文件
     */
    @Test
    public void test4(){
        //1、创建File类对象
        File file1 = new File("hello.txt");
        File file2 = new File("hello_copy.txt");

        //2、创建输入、输出流
        FileReader fr = null;
        FileWriter fw = null;

        try {

            fr = new FileReader(file1);
            fw = new FileWriter(file2);

            //3、复制操作
            char[] charsBuffer = new char[5];
            //记录每次记录到charsBuffer字符的个数
            int len;
            while((len=fr.read(charsBuffer))!=-1){
                fw.write(charsBuffer, 0 ,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

                //4、关闭流
            try {
                if(fr!=null)
                    fr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if(fw!=null)
                    fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
